public class demo1 {
    class Solution {
        public int maxTurbulenceSize(int[] arr) {
            int n=arr.length;
            int[]f=new int[n+1];//表示该位置状态为上升趋势的最长湍子数组
            int[]g=new int[n+1];
            f[0]=g[0]=1;
            int ret=1;
            for(int i=1;i<n;i++){
                if(arr[i-1]>arr[i]){
                    f[i]=g[i-1]+1;
                    g[i]=1;
                }else if(arr[i-1]<arr[i]){
                    f[i]=1;
                    g[i]=f[i-1]+1;
                }else{
                    f[i]=g[i]=1;
                }
                ret=Math.max(ret,Math.max(f[i],g[i]));
            }
            return ret;
        }
    }
}
